import axios from "axios";

const url = "https://jishuba.cn/wp-json/wx/v1/get-user"; // Replace with your actual URL

class WxAuthClient {
  getUser = async () => {
    try {
      const response = await axios.get(url);
      return response.data;
    } catch (error) {
      console.error("Error fetching user data:", error);
      throw error;
    }
  };

  saveUser(user) {
    try {
      localStorage.setItem("ic_user", JSON.stringify(user));
    } catch (error) {
      console.error("Error saving user to localStorage:", error);
      throw error;
    }
  }

  getUserLocal() {
    try {
      const user = localStorage.getItem("ic_user");
      return user ? JSON.parse(user) : null;
    } catch (error) {
      console.error("Error getting user from localStorage:", error);
      throw error;
    }
  }

  isLoggedin() {
    return !!this.getUserLocal();
  }

  logout() {
    try {
      localStorage.removeItem("ic_user");
    } catch (error) {
      console.error("Error clearing user from localStorage:", error);
      throw error;
    }
  }

}
const wxAuthClient = new WxAuthClient();
export default wxAuthClient;
